// ==PREPROCESSOR==
// @name "DarkOne Display Panel"
// @version "3.1"
// @author "super-gau and tedGo, includes partial codes by fbuser and T.P Wang"
// @import "%fb2k_path%themes\DarkOne\Others\WSH Scripts\DO Global Script.js"
// ==/PREPROCESSOR==

window.MinWidth = 280;

var g_down = false, v_change = false;
var g_timer = null, v_timer = null;
var g_interval, fileinfo;

// ----- COLOURS -----
var d_arr = new Array("Default", "Blue", "Green", "Red", "White", "Yellow");
var c_arr = new Array(RGBA(191, 228, 255, 255), RGBA(128, 192, 255, 255), RGBA(0, 255, 0, 255), RGBA(255, 0, 0, 255), RGBA(255, 255, 255, 255), RGBA(255, 255, 0, 255));

function toRGB(d){
	var d, r, g, b;
	d = d - 0xff000000; r = d >> 16; g = d >> 8 & 0xFF; b = d & 0xFF;
	return [r,g,b];
}

function combColours(c1, c2, f){
	var c1, c2;
	c1 = toRGB(c1);	c2 = toRGB(c2);

	var r, g, b;
	r = Math.round(c1[0] + f * (c2[0] - c1[0])); g = Math.round(c1[1] + f * (c2[1] - c1[1])); b = Math.round(c1[2] + f * (c2[2] - c1[2]));
	return (0xff000000 | (r << 16) | (g << 8) | (b));
}
function InitColours() {
	d_col = window.GetProperty("Display Colour", 0);
	g_dcol = d_arr[d_col];
	g_acol = c_arr[d_col];
	g_fact = g_acol == -65536 ? 0.09 : 0.07;
	g_bcol = combColours(ui_backcol, g_acol, g_fact);
}

InitColours();

// ----- CREATE HELPERS -----
String.prototype.repeat = function(n) {
	return (new Array(n + 1)).join(this);
}

function pad(x, y, z) {
	z || (z = ' ');
	return x.length < y ? x + z.repeat(y - x.length) : x;
}

function pad_right(x, y, z) {
	z || (z = ' ');
	return x.length < y ? z.repeat(y - x.length) + x : x
}

function TimeFmt(t) {
	var zpad = function(n) {
		var str = n.toString();
		return str.length < 2 ? "0" + str : str;
	}

	var h = Math.floor(t / 3600);
	t -= h * 3600;
	var m = Math.floor(t / 60);
	t -= m * 60;
	var s = Math.floor(t);

	if (h > 0) return h.toString() + "h" + zpad(m) + "m";

	return m.toString() + ":" + zpad(s);
}
function drawSquares(gr, squareSize, paddingSize, x, y, w, h) {
	var stepSize = squareSize + paddingSize;
	var totalsq = Math.ceil(w / stepSize);
	var totalrow = Math.ceil(h / stepSize);

	for(var j = 0; j < totalrow; j++) {
		for(var i = 0; i < totalsq; i++) {
			gr.FillSolidRect(i * stepSize + x, j * stepSize + y, squareSize, squareSize, g_bcol);
		}
	}
}
// ----- CREATE SEEKBAR -----
var g_img1 = gdi.Image(configPath + "Images\\SliderPlay.png");
var g_img2 = gdi.Image(configPath + "Images\\Slider.png");

function Seekbar(x, y, w, h) {
	this.left = x;
	this.top = y;
	this.w = w;
	this.h = h;
	this.right = x + w;
	this.bottom = y + h;
	this.g_drag_seek = 0;
	this.margin = 30;
	this.area = this.w - (this.margin * 2);

	this.traceMouse = function(x, y, onlyWidth) {
		if (onlyWidth == null) onlyWidth = true;
		return this.left < x && x < this.right && (onlyWidth || this.top < y && y < this.bottom);
	}

	this.draw = function(gr) {
		var c, f;

		if (this.active) {
			f = g_down ? this.g_drag_seek : fb.PlaybackTime / fb.PlaybackLength;
			c = g_img1;
			f = this.margin + this.area * f;
			f = f > 0 ? f : this.margin;
		} else {
			c = g_img2;
			f = this.margin;
		}

		gr.DrawRoundRect(this.left + 2, this.top + 1, this.w - 3, this.h - 1, 2, 2, 1, RGBA(255, 255, 255, 32));
		gr.DrawRoundRect(this.left, this.top, this.w - 2, this.h - 1, 2, 2, 1, RGBA(0, 0, 0, 255));
		gr.DrawRect(this.left + 1, this.top + 1, this.w - 4, this.h - 3, 1, RGBA(0, 0, 0, 255));
		gr.DrawRect(this.left + 2, this.top + 2, this.w - 4, this.h - 3, 1, RGBA(25, 40, 51, 255));
		if (!window.IsTransparent) gr.FillSolidRect(this.left + 2, this.top + 2, this.w - 4, this.h - 3, ui_backcol);
		c && gr.DrawImage(c, f - 28, this.top + 2, 56, this.h - 2, 0, 0, c.Width, c.Height);
	}

	this.Repaint = function() {
		window.RepaintRect(this.left, this.top, this.w, this.h);
	}

	this.calcDragSeek = function(x, y) {
		this.g_drag_seek = (x - this.margin) / this.area;
		this.g_drag_seek = this.g_drag_seek < 0 ? 0 : this.g_drag_seek < 1 ? this.g_drag_seek : 1;
	}

	this.on_mouse_lbtn_down = function(x, y) {
		if (this.traceMouse(x, y, false) && this.active) {
			g_down = true;
			this.calcDragSeek(x, y);
			this.Repaint();
		}
	}

	this.on_mouse_lbtn_up = function(x, y) {
		if (g_down && this.active) {
			this.calcDragSeek(x, y);
			fb.PlaybackTime = fb.PlaybackLength * this.g_drag_seek;
		}
	}

	this.on_mouse_move = function(x, y) {
		if (g_down && this.traceMouse(x, y) && this.active) {
			this.calcDragSeek(x, y);
			window.Repaint();
		}

		this.mouseX = x;
		this.mouseY = y;
	}

	this.on_mouse_wheel = function(delta) {
		if (this.traceMouse(this.mouseX, this.mouseY, false) && this.active) fb.PlaybackTime += delta
	}

	this.on_playback_new_track = function(metadb) {
		if (metadb) this.active = fb.PlaybackLength <= 0 || metadb.RawPath.indexOf("FOO_LASTFM") == 0 ? false : true;

		if (g_timer) {
			window.ClearInterval(g_timer);
			g_timer = null;
		}

		if (this.active) {
			g_interval = Math.max(Math.round(fb.PlaybackLength / (this.area / 100)), 40);
			g_timer = window.SetInterval(function () {seekbar.Repaint();}, g_interval);
		}
	}

	this.getElapseTime = function() {
		return TimeFmt(this.g_drag_seek * fb.PlaybackLength);
	}

	this.getRemainTime = function() {
		return TimeFmt(fb.PlaybackLength - (this.g_drag_seek * fb.PlaybackLength));
	}

	this.init = function() {
		this.on_playback_new_track(fb.GetNowPlaying());
	}
}

// ----- CREATE INFO OPTIONS -----

Info.prototype.setColours = function() {
	var metadb = fb.GetNowPlaying();

	if (metadb) {
		fileinfo = metadb.GetFileInfo();
		this.Colours[0] = fileinfo.InfoValue(fileinfo.InfoFind("encoding")) == "lossless" ? g_acol : g_bcol;
		this.Colours[1] = fileinfo.InfoValue(fileinfo.InfoFind("encoding")) == "lossless" ? g_bcol : g_acol;
		this.Colours[2] = fileinfo.InfoValue(fileinfo.InfoFind("md5")) ? g_acol : g_bcol;
		this.Colours[3] = fb.TitleFormat("[%replaygain_track_gain%]").Eval() ? g_acol : g_bcol;
		this.Colours[4] = fileinfo.MetaValue(fileinfo.MetaFind("tracknumber"), 0) ? ui_btntxtcol : g_bcol;
		this.Colours[5] = fileinfo.MetaValue(fileinfo.MetaFind("totaltracks"), 0) ? ui_btntxtcol : g_bcol;
		this.Colours[6] = fb.PlaybackLength <= 0 ? g_bcol : ui_btntxtcol;
		this.Colours[7] = ui_btntxtcol;
	} else for (var i = 0; i < this.Colours.length; i++) this.Colours[i] = g_bcol;
}

Info.prototype.setTrackNo = function() {
	var metadb = fb.GetNowPlaying();
	var a;

	if (metadb) {
		fileinfo = metadb.GetFileInfo();
		var b = fileinfo.MetaValue(fileinfo.MetaFind("tracknumber"), 0);
		var c = fileinfo.MetaValue(fileinfo.MetaFind("totaltracks"), 0);
		var d = b ? pad_right(b, 2, "0") : "";
		var e = c ? pad_right(c, 2, "0") : "";
		if (b) a = pad(d, 4) + e;
		else if (fb.PlaybackLength <= 0) a = "Stream";
		else if (metadb.RawPath.indexOf("FOO_LASTFM") == 0) a = "last.fm";
		this.TrackNo = a;
	} else this.TrackNo = "";
}

Info.prototype.setPBTime = function() {
	this.Elapse = TimeFmt(fb.PlaybackTime);
	this.Remain = fb.PlaybackLength > 0 ? TimeFmt(fb.PlaybackLength - fb.PlaybackTime) : "";
}

Info.prototype.setBitrate = function() {
	this.Bitrate = fb.TitleFormat("%bitrate%").Eval();
}

Info.prototype.setInfo = function() {
	this.setColours();
	this.setTrackNo();
	this.setPBTime();
	this.setBitrate();
}

function Info() {
	this.Colours = new Array(8);
	this.setInfo();
}

info = new Info();

// ----- CREATE MENU -----
function CustomMenu(x, y) {
	var a = window.CreatePopupMenu();
	var idx;

	for (var i = 0; i < d_arr.length; i++) {
		a.AppendMenuItem(0, 1 + i, d_arr[i]);
	}
	a.CheckMenuRadioItem(1, 6, d_col + 1);

	a.AppendMenuSeparator();
	a.AppendMenuItem(0, 7, "Properties");
	a.AppendMenuItem(0, 8, "Configure...");

	idx = a.TrackPopupMenu(x, y);

	switch (true) {
		case (idx >= 1 && idx <= 6):
			window.SetProperty("Display Colour", idx - 1);
			InitColours();
			info.setColours();
			
			window.Repaint();
			break;

		case (idx == 7):
			window.ShowProperties();
			break;

		case (idx == 8):
			window.ShowConfigure();
			break;
	}

	a.Dispose();
}

// ----- DRAW -----
function on_paint(gr) {
	var t_elp = g_down && seekbar.active ? seekbar.getElapseTime() : info.Elapse;
	var t_rem = g_down && seekbar.active ? seekbar.getRemainTime() : info.Remain;
	var p_icon;

	if (!window.IsTransparent) gr.FillSolidRect(0, 0, ww, wh, ui_backcol);
	seekbar.draw(gr);

	if (fb.IsPlaying) p_icon = fb.IsPaused ? "\u258c" : "\u25ba";

	gr.DrawRect(1, 18, ww * 17 / 70, 10, 1, info.Colours[0]);
	gr.DrawRect(ww * 71 / 280, 18, ww * 17 / 70, 10, 1, info.Colours[1]);
	gr.DrawRect(ww * 141 / 280, 18, ww * 17 / 70, 10, 1, info.Colours[2]);
	gr.DrawRect(ww * 211 / 280, 18, ww * 17 / 70, 10, 1, info.Colours[3]);

	gr.GdiDrawText("LOSSLESS", btn_font, info.Colours[0], 2, 18, ww * 17 / 70, 10, 37);
	gr.GdiDrawText("LOSSY", btn_font, info.Colours[1], ww * 9 / 35, 18, ww * 17 / 70, 10, 37);
	gr.GdiDrawText("AUDIO MD5", btn_font, info.Colours[2], ww * 71 / 140, 18, ww * 17 / 70, 10, 37);
	gr.GdiDrawText("REPLAYGAIN", btn_font, info.Colours[3], ww * 53 / 70, 18, ww * 17 / 70, 10, 37);

	gr.GdiDrawText("TRACK", btn_font, info.Colours[4], 1, 30, 27, 10, 37);
	gr.GdiDrawText("TOTAL", btn_font, info.Colours[5], 41, 30, 27, 10, 37);
	gr.GdiDrawText("ELAPSED", btn_font, v_change ? g_bcol : info.Colours[7], ww - 155, 30, 35, 10, 38);
	gr.GdiDrawText("REMAIN", btn_font, v_change ? g_bcol : info.Colours[6], ww - 90, 30, 30, 10, 38);
	gr.GdiDrawText("dB", btn_font, v_change ? ui_btntxtcol : g_bcol, ww - 45, 30, 10, 10, 37);
	gr.GdiDrawText("KBPS", btn_font, v_change ? g_bcol : info.Colours[7], ww - 20, 30, 20, 10, 37);
    
	gr.GdiDrawText("\u2588".repeat(Math.ceil(ww / 10)), dsp_font, g_bcol, 0, 38, ww, 19, 37);
	gr.GdiDrawText(info.TrackNo, dsp_font, g_acol, 0, 38, 90, 19, 36);
	gr.GdiDrawText(p_icon, dsp_font, g_acol, (ww / 2) - 50, 38, 10, 19, 37);
	if (v_change) gr.GdiDrawText("Volume " + fb.Volume.toFixed(2), dsp_font, g_acol, ww - 180, 38, 180, 19, 38);
	else {
		fb.IsPlaying && gr.GdiDrawText(t_elp, dsp_font, g_acol, ww - 180, 38, 60, 19, 38);
		gr.GdiDrawText(t_rem, dsp_font, g_acol, ww - 120, 38, 60, 19, 38);
		gr.GdiDrawText(info.Bitrate, dsp_font, g_acol, ww - 60, 38, 60, 19, 38);
	}
}

// ----- MOUSE ACTIONS -----
function on_mouse_move(x, y) {
	seekbar.on_mouse_move(x, y);
}

function on_mouse_lbtn_down(x, y) {
	seekbar.on_mouse_lbtn_down(x, y);
	window.RepaintRect(ww - 180, 38, 120, 19);
}

function on_mouse_lbtn_up(x, y) {
	seekbar.on_mouse_lbtn_up(x, y);
	g_down = false;
}

function on_mouse_wheel(delta) {
	seekbar.on_mouse_wheel(delta);

	if (seekbar.traceMouse(seekbar.mouseX, seekbar.mouseY, false)) {
		info.setPBTime();
		window.RepaintRect(ww - 180, 38, 120, 19);
	}
}
function on_mouse_rbtn_up(x, y) {
	CustomMenu(x, y);
	return true;
}
// ----- EVENTS -----
function on_size() {
	ww = window.Width;
	wh = window.Height;
	seekbar = new Seekbar(0, 0, ww, 8);
	seekbar.init();
}

function on_volume_change(val) {
	if (v_timer) {
		window.ClearTimeout(v_timer);
		v_timer = null;
	}

	v_timer = window.SetTimeout(
		function () {
			window.RepaintRect(ww - 180, 30, 180, 27);
			window.ClearTimeout(v_timer);
			v_timer = null;
			v_change = false;
		}, 3000);

	v_change = true;
	window.RepaintRect(ww - 180, 30, 180, 27);
}

function on_playback_seek(time) {
	seekbar.Repaint();
}

function on_playback_time(time) {
	info.setPBTime();
	window.RepaintRect(ww - 180, 38, 120, 19);
}

function on_playback_dynamic_info() {
	info.setBitrate()
	window.RepaintRect(ww - 60, 38, 60, 19);
}

function on_playback_edited() {
	info.setInfo();
	window.Repaint();
}

function on_playback_new_track(metadb) {
	seekbar.on_playback_new_track(metadb);
	info.setInfo();
	window.Repaint();
}

function on_playback_pause(state) {
	window.RepaintRect((ww / 2) - 50, 38, 10, 19);
}

function on_playback_stop(reason) {
	if (fileinfo) fileinfo.Dispose();

	if (g_timer) {
		window.ClearInterval(g_timer);
		g_timer = null;
	}

	if (reason != 2) {
		seekbar.active = false;
		info.setInfo();
	}

	window.Repaint();
}